#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <windows.h>
#include <openssl/aes.h>
#include <openssl/rand.h>

std::vector<unsigned char> readFile(const std::string& filename) {
    std::ifstream file(filename, std::ios::binary);
    return std::vector<unsigned char>((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
}

void writeFile(const std::string& filename, const std::vector<unsigned char>& data) {
    std::ofstream file(filename, std::ios::binary);
    file.write(reinterpret_cast<const char*>(data.data()), data.size());
}

std::vector<unsigned char> encrypt(const std::vector<unsigned char>& data, const std::string& key) {
    std::vector<unsigned char> iv(AES_BLOCK_SIZE);
    RAND_bytes(iv.data(), AES_BLOCK_SIZE);

    AES_KEY aes_key;
    AES_set_encrypt_key(reinterpret_cast<const unsigned char*>(key.data()), 128, &aes_key);

    std::vector<unsigned char> encrypted(data.size() + AES_BLOCK_SIZE);
    std::copy(iv.begin(), iv.end(), encrypted.begin());

    AES_cbc_encrypt(data.data(), encrypted.data() + AES_BLOCK_SIZE, data.size(), &aes_key, iv.data(), AES_ENCRYPT);

    return encrypted;
}

int main(int argc, char* argv[]) {
    if (argc != 4) {
        std::cerr << "Usage: " << argv[0] << " <input_file> <output_file> <key>" << std::endl;
        return 1;
    }

    std::string input_file = argv[1];
    std::string output_file = argv[2];
    std::string key = argv[3];

    std::vector<unsigned char> data = readFile(input_file);
    std::vector<unsigned char> encrypted = encrypt(data, key);
    writeFile(output_file, encrypted);

    std::cout << "Encryption complete. Output file: " << output_file << std::endl;

    return 0;
}
